﻿//模块名称:系统上传控件
//功能描述:
//创建人:  Lam
//创建时间:2010年4月28日16:59:19
//修改描述:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using webnew.Common;
using System.Web.Services;
namespace webnew.UserControls
{
    public partial class Uc_FileUpload : System.Web.UI.UserControl
    {
        /////页面调用该上传控件的例子,以邮件的附件为例：
        //////if (!Page.IsPostBack)
        //////    {              
        //////      //设置邮件附件的上传路径
        //////      string FilePathWithoutFileName = ConfigurationSettings.AppSettings["mail_upload"] + DateTime.Now.ToString("yyyyMM") + @"\";
        //////      //设置邮件附件的上传类型
        //////      string ValidFileType = ConfigurationSettings.AppSettings["MailFileType"];
        //////      //设置邮件附件大小的最大长度
        //////      int MaxFileSize = Convert.ToInt32(ConfigurationSettings.AppSettings["MailMaxFileSize"]);
        //////      //显示上传控件
        //////      Uc_FileUpload1.UcShow("Sys_company", "1", FilePathWithoutFileName, ValidFileType, MaxFileSize);
        //////    }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //记录上传的文件列表
            //新建文件列表
            FileList = new List<Model.Sys_FilesInfo>();
            
            if (!Page.IsPostBack)
            {              
                webnew.Common.CommonHelper.DataGrdBind(grdFileList, ((List<Model.Sys_FilesInfo>)ViewState["FileList"]));
            }
        }

        protected void OnUnload(object sender, EventArgs e)
        {
            ViewState["FileList"] = null;
        }
       
        /// <summary>
        /// 显示上传控件
        /// </summary>
        /// <param name="tblName">上传下载对应的表名</param>
        /// <param name="tblId">对应表名的主键ID</param>
        /// <param name="uploadPath">上传的路径</param>
        /// <param name="uploadType">允许上传文件的类型</param>
        /// <param name="MaxFileSize">容许上传文件的大小</param>
        public void UcShow(string tblName, string tblId,string uploadPath,string uploadType,int MaxFileSize)
        {
            this.tblName = tblName;
            this.tblID = tblId;
            this.FilePathWithoutFileName = uploadPath;
            this.ValidFileType = uploadType;
            this.MaxFileSize = MaxFileSize;
            //初始化上传类
            //UploadFile uploadfile = new UploadFile("", false, ValidFileType, MaxFileSize);
           
        }

       
        
        

        /// <summary>
        /// 对应表名
        /// </summary>
        private string tblName
        {
            get { return ViewState["tblName"].ToString(); }
            set
            {
                this.ViewState["tblName"] = value;

            }
        }
        /// <summary>
        /// 对应表ID
        /// </summary>
        private string tblID
        {
            get { return ViewState["tblID"].ToString(); }
            set
            {
                this.ViewState["tblID"] = value;
            }
        }

        /// <summary>
        /// 容许上传文件的大小
        /// </summary>
        private int MaxFileSize
        {
            get { return Convert.ToInt32(ViewState["MaxFileSize"]); }
            set 
            {
                this.ViewState["MaxFileSize"] = value;
            }
        }
        /// <summary>
        /// 允许上传文件的类型
        /// </summary>
        public string ValidFileType
        {
            get { return ViewState["ValidFileType"].ToString(); }
            set
            {
                this.ViewState["ValidFileType"] = value;
            }

        }


        /// <summary>
        /// 文件存放路径
        /// </summary>
        public string FilePathWithoutFileName
        {

            get { return ViewState["FilePathWithoutFileName"].ToString()+ DateTime.Now.ToString("yyyyMM") + @"\"; }
            set
            {
                this.ViewState["FilePathWithoutFileName"] = value;
            }
        }

        /// <summary>
        /// 保存上传的信息列表
        /// </summary>
        public List<Model.Sys_FilesInfo> FileList
        {
            get;
            set;
        }

        ///// <summary>
        ///// AJAX调用上传事件
        ///// </summary>
        //[WebMethod]
        //public static void Upload()
        //{
        //     _Upload();
        //}


        public void _Upload()
        {
            //初始化上传类
            UploadFile uploadfile = new UploadFile("", false, ValidFileType, MaxFileSize);
            //执行上传
            uploadfile.ExecUploadFile(FilePathWithoutFileName, infUpFile);
            //如果上传成功
            if (uploadfile.UploadState)
            {                      
                Model.Sys_FilesInfo model = new Model.Sys_FilesInfo();
                model.FileHref = "<a href='" + uploadfile.PathFullName + "'>" + UploadFile.NewFileToOldFile(uploadfile.FileName) + "</a>";
                model.FileName = uploadfile.FileName;
                model.FileType = uploadfile.FileExtName;
                model.TblID = tblID;
                model.FilePath = uploadfile.PathFullName;
                model.FileLength = Convert.ToDecimal(uploadfile.FileSize);
                model.TblName = tblName;

                FileList.Add(model);
                //把上传的文件信息存在ViewState["FileList"]对象中
                if ((ViewState["FileList"] == null))
                {
                    ViewState["FileList"] = FileList;
                }
                else
                {
                    ((List<Model.Sys_FilesInfo>)ViewState["FileList"]).Add(model);
                }
                //ViewState["FileList"]对象中的文件列表信息赋给控件的属性文件列表中
                FileList = ((List<Model.Sys_FilesInfo>)ViewState["FileList"]);
                //重新绑定文件列表并显示
                webnew.Common.CommonHelper.DataGrdBind(grdFileList, ((List<Model.Sys_FilesInfo>)ViewState["FileList"]));
            }
            
            //grdFileList.
            //显示上传的信息
            lbMsg.Text = uploadfile.UploadInfo;
           // return uploadfile.UploadInfo;
        }


        protected void btnUpload_Click(object sender, EventArgs e)
        {
            _Upload();                  
        }

       
        /// <summary>
        /// 删除指定的文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdFileList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string selValue = grdFileList.DataKeys[e.RowIndex]["FilePath"].ToString();
            if (selValue == "")
            {                
                return;
            }
            else
            {
                switch (UploadFile.DeleteFile(selValue))
                {
                    //返回0,表示删除失败
                    case 0:
                        lbMsg.Text = "删除失败！";
                        break;

                    //返回1,表示删除成功
                    case 1:
                        // 删除某个选定的ViewState["FileList"])上传文件的列表信息                    
                        ((List<Model.Sys_FilesInfo>)ViewState["FileList"]).RemoveAt(e.RowIndex);
                        //删除文件列表的选定行
                        grdFileList.DeleteRow(e.RowIndex);
                        //重新绑定文件列表
                        webnew.Common.CommonHelper.DataGrdBind(grdFileList, ((List<Model.Sys_FilesInfo>)ViewState["FileList"]));
                        lbMsg.Text = "删除成功！";
                        break;

                    //返回2,则表示该文件不存在
                    case 2:
                        lbMsg.Text = "该文件不存在服务器上！";
                        break;
                    default:
                        break;
                }
            }
        }

        //对文件列表进行处理
        protected void grdFileList_DataBound(object sender, EventArgs e)
        {
           //文件名
            foreach(GridViewRow gvRow in grdFileList.Rows)
           {
              gvRow.Cells[1].Text=UploadFile.NewFileToOldFile(gvRow.Cells[1].Text);
              gvRow.Cells[1].Text +=" (" + (Convert.ToDouble(grdFileList.DataKeys[gvRow.RowIndex]["FileLength"])).ToString("F2")+"K)";
           }
            		 
	    }
        /// <summary>
        /// 保存上传文件的数据
        /// </summary>
        /// <returns>返回0:表示保存失败,返回1,表示保存成功</returns>
        public int SavaFileDate()
        {
            if (ViewState["FileList"] != null)
            {
                return new BLL.Sys_Files().Add(((List<Model.Sys_FilesInfo>)ViewState["FileList"]));
            }
            else
                return 0;
           
        }
    }
}